#define DEBUG
#include <cstdio>
#define MAXN 5000

using namespace std;

int max(int a, int b) {
  return a>b ? a : b;
}

int main() {
  freopen("b.in", "r", stdin);
  freopen("b.out", "w", stdout);

  static int l[MAXN+1], r[MAXN+1], f[MAXN+1][MAXN+1];
  int n;
  scanf("%d", &n);
  for (int i=1; i<=n; i++) {
    scanf("%d %d", l+i, r+i);
  }

  for (int i=1; i<=n; i++) {
    for (int j=l[i]; j<=r[i]; j++) {
      f[i][j] = max(f[i-1][j-1]+1, max(f[i][j-1], f[i-1][j]));
    }
    for (int j=r[i]+1; j<=n; j++) {
      f[i][j] = f[i][j-1];
    }
  }

  printf("%d", f[n][n]);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
